<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app" class="demo-input-size">
    <div style="padding-bottom: 20px;">
        <el-input
                v-model="searchInfo.aname"
                size="medium"
                placeholder="请输入姓名"
                suffix-icon=""
                style="display:inline-block;width: 200px;">
        </el-input>
        <el-input
                v-model="searchInfo.asex"
                size="medium"
                placeholder="请输入性别"
                suffix-icon=""
                style="display:inline-block;width: 200px;">
        </el-input>
        <el-input
                v-model="searchInfo.rname"
                size="medium"
                placeholder="请输入职位"
                suffix-icon=""
                style="display:inline-block;width: 200px;">
        </el-input>
        <el-input
                v-model="searchInfo.dname"
                size="medium"
                placeholder="请输入部门"
                suffix-icon=""
                style="display:inline-block;width: 200px;">
        </el-input>
        <el-button type="primary" @click="searchAdminInfo()">查询</el-button>
        <el-button type="success" @click="showAddDialog()">添加</el-button>
        <el-button type="warning" @click="uploadAdminInfo()">上传</el-button>
        <el-button  circle @click="downloadAdminDemo()" >模</el-button>
        <form id="upform" style="display: inline-block">
            <input name="downloadAdminDemo" type="file">
        </form>
    </div>
    <div style="
				width: 1020px;
				height: 600px;
				margin: auto;
				">
        <el-table
            :data="adminArr"
            border
            style=" width: 100%; margin: auto; width:915px;">
            <el-table-column
                    prop="id"
                    label="编号"
                    width="80">
            </el-table-column>
            <el-table-column
                    prop="aname"
                    label="姓名"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="aphone"
                    label="手机号"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="role.rname"
                    label="职位"
                    width="80">
            </el-table-column>
            <el-table-column
                    prop="dept.dname"
                    label="部门"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="office.oname"
                    label="科室"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="education.education"
                    label="学历"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop=""
                    label="编辑"
                    width="190">
                <template slot-scope="scope">
                    <el-button type="success" @click="showUpdateDialog(scope.row.id)" plain>修改</el-button>
                    <el-button type="danger" @click="deleteAdmin(scope.row.id)" plain>删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            align=center
            :total="totalCount"
            :page-size="pagesize">
        </el-pagination>
    </div>

    <el-dialog title="添加员工" :visible.sync="dialogFormVisible">
        <el-form id="adminFrom" :model="adminInfo" :rules="rules" ref="adminInfo">
            <el-form-item label="员工姓名" prop="id"  style="display: none">
                <el-input v-model="adminInfo.id" name="id" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="员工姓名" prop="aname">
                <el-input v-model="adminInfo.aname" name="aname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="员工手机号" prop="aphone">
                <el-input oninput="this.value=this.value.replace(/\D/g,'')" maxlength="11" v-model="adminInfo.aphone" name="aphone" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="请选择部门" prop="did">
                <template>
                    <select style="width: 80px;" v-model="adminInfo.did" placeholder="请选择部门" :value="adminInfo.id" @change="listOffice(adminInfo.did)" name="did">
                        <option  v-for="(g,i) in doption"
                                 :label="g.dname"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
            <el-form-item label="请选择科室" prop="oid">
                <template>
                    <select style="width: 80px;" v-model="adminInfo.oid"  placeholder="请选择科室" @change="listRole(adminInfo.oid)" name = "oid">
                        <option  v-for="(g,i) in ooption"
                                 :label="g.oname"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
            <el-form-item label="请选择级别" prop="rid">
                <template>
                    <select style="width: 80px;" v-model="adminInfo.rid" placeholder="请选择级别" name = "rid">
                        <option  v-for="(g,i) in roption"
                                 :label="g.rname"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
            <el-form-item label="请选择学历" prop="education">
                <template>
                    <select style="width: 80px;" v-model="adminInfo.education" placeholder="请选择学历" name = "education">
                        <option  v-for="(g,i) in edoption"
                                 :label="g.education"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addAdmin()">添 加</el-button>
        </div>
    </el-dialog>

    <el-dialog title="修改员工信息" :visible.sync="updateDialogFormVisible">
        <el-form id="updateAdminFrom" :model="adminInfo" :rules="rules" ref="adminInfo">
            <el-form-item label="编号" prop="id" style="display: none">
                <el-input v-model="adminInfo.id" name="id" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="员工姓名" prop="aname">
                <el-input v-model="adminInfo.aname" name="aname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="员工手机号" prop="aphone">
                <el-input oninput="this.value=this.value.replace(/\D/g,'')" maxlength="11" v-model="adminInfo.aphone" name="aphone" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="请选择部门" prop="did">
                <template>
                    <select style="width: 80px;" v-model="adminInfo.did" placeholder="请选择部门" name="did" @change="listOffice(adminInfo.did)">
                        <option  v-for="(g,i) in doption"
                                 :label="g.dname"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
            <el-form-item label="请选择科室" prop="oid">
                <template>
                    <select style="width: 80px;" v-model="adminInfo.oid"  placeholder="请选择科室" name = "oid" @change="listRole(adminInfo.oid)">
                        <option  v-for="(g,i) in ooption"
                                 :label="g.oname"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
            <el-form-item label="请选择级别" prop="rid">
                <template>
                    <select style="width: 80px;" v-model="adminInfo.rid" placeholder="请选择级别" name = "rid">
                        <option  v-for="(g,i) in roption"
                                 :label="g.rname"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
            <el-form-item label="请选择学历" prop="education">
                <template>
                    <select style="width: 80px;" v-model="adminInfo.education" placeholder="请选择学历" name = "education">
                        <option  v-for="(g,i) in edoption"
                                 :label="g.education"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="updateDialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateAdmin()">修 改</el-button>
        </div>
    </el-dialog>
</div>
<script>
    $.get("/demo/adminInfo/queryEducation",function (backData) {
        haha.edoption = backData.data;
    });
    var haha = new Vue({
        el:"#app",
        data:{
            education:"",
            edoption:[],
            rid:"",
            did:"",
            oid:"",
            roption:[],
            doption:[],
            ooption:[],
            adminArr:[],
            adminInfo:{},
            searchInfo:{
                aname:"",
                asex:"",
                dname:"",
                rname:""

            },
            totalCount:0,
            pagesize:7,
            dialogFormVisible:false,
            updateDialogFormVisible:false,
            rules:{
                aname: [
                    { required: true, message: '请输入员工姓名', trigger: 'blur' }
                ],
                did: [
                    { required: true, message: '请输入员工部门', trigger: 'blur' }
                ],
                oid: [
                    { required: true, message: '请输入员工科室', trigger: 'blur' }
                ],
                rid: [
                    { required: true, message: '请输入员工等级', trigger: 'blur' }
                ],
                education: [
                    { required: true, message: '请输入员工学历', trigger: 'blur' }
                ],
                aphone:[
                    { required: true, message: '请输入员工手机号', trigger: 'blur' },
                    { min: 11, max: 11, message: '长度在 3 到 18 个字符', trigger: 'blur'}

                ]
            }
        },
        methods:{
            updateAdmin(){
                haha.$refs["adminInfo"].validate((valid) => {
                    if (valid) {
                        var adminDataUpdate = new FormData(updateAdminFrom);
                        $.ajax({
                            url:"/demo/adminInfo/update",
                            type:"POST",
                            data:adminDataUpdate,
                            processData:false,
                            contentType:false,
                            success:function(backData){
                                haha.updateDialogFormVisible=false;
                                getData(1,7);
                            }
                        });
                    }
                });
            },
            showUpdateDialog(id){
                $.get("/demo/adminInfo/getAdminInfo/"+id,function (backData) {
                    haha.adminInfo = backData.data;
                    $.get("/demo/adminInfo/getOname/" ,function (backData) {
                        haha.ooption = backData.data;
                    });
                    $.get("/demo/adminInfo/getDname/" ,function (backData) {
                        haha.doption = backData.data;
                    });
                    $.get("/demo/adminInfo/getRname/" ,function (backData) {
                        haha.roption = backData.data;
                    });
                    haha.updateDialogFormVisible = true;
                });
            },
            showAddDialog(){
                $.get("/demo/adminInfo/queryDname",function (backData) {
                    haha.doption = backData.data;
                    haha.dialogFormVisible = true;
                });
            },
            addAdmin(){
                haha.$refs["adminInfo"].validate((valid) => {
                    if (valid) {
                        var addAdminData = new FormData(adminFrom);
                        $.ajax({
                            url:"/demo/adminInfo/add",
                            type:"POST",
                            data:addAdminData,
                            processData:false,
                            contentType:false,
                            success:function(backData){
                                if(backData.code == 1){
                                    haha.$message.success('添加成功');
                                    haha.dialogFormVisible=false;
                                    getData(1,7);
                                }else {
                                    haha.$message.error(backData.msg);
                                }
                            }
                        });
                    }
                });
            },
            listOffice(did){
                $.get("/demo/adminInfo/queryOname/"+did ,function (backData) {
                    haha.ooption = backData.data;
                });
            },
            listRole(oid){
                $.get("/demo/adminInfo/queryRname/"+oid ,function (backData) {
                    haha.roption = backData.data;
                });
            },
            deleteAdmin(id){
                haha.$confirm('此操作将永久删除该医生, 是否继续?', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.get("/demo/adminInfo/delete/"+id ,function (backData) {
                        haha.$message.success(backData.msg);
                        getData(1,7);
                    });
                });
            },
            searchAdminInfo(){
                getData(1,7);
            },
            handleCurrentChange( val  ){
                getData(val,7);
            },
            downloadAdminDemo(){
                window.location.href = "/demo/admin/员工信息表（模板）.xlsx";
            },
            uploadAdminInfo(){
                // 1 使用js写好的 FormData类  读取表单中的信息
                var  adminData = new FormData(upform);
                // 2 通过 ajax 将 data 发送给后台  $.post
                $.ajax({
                    url:"/demo/adminsource/upload",
                    type:"POST",
                    data:adminData,
                    processData:false,
                    contentType:false,
                    success:function(backData){
                        if (backData.code == 1){
                            haha.$message.success('添加成功');
                        } else {
                            haha.$message.error(backData.msg);
                        }
                    }
                });

            },
        }
    });
    function getData( a , b ) {
        haha.searchInfo.pageNO = a;
        haha.searchInfo.pageCount = b;
        $.post("/demo/adminInfo/query",haha.searchInfo,function (backData) {
            haha.adminArr = backData.data.currentData;
            haha.totalCount = backData.data.totalCount;
        });
    }
    getData(1,7);
</script>
</body>
</html>